package city.spring.modules.bed.controller;

import city.spring.modules.bed.entity.RoomTypeEntity;
import city.spring.modules.bed.service.RoomTypeService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：房间类型
 *
 * @author 潘小梅
 * @date 2020-04-08 11:34:42
 */
@RestController
@RequestMapping("/room_type")
public class RoomTypeController extends ApiController {
    private final RoomTypeService roomTypeService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<RoomTypeEntity, ?>> entityOrderFields;

    public RoomTypeController(RoomTypeService roomTypeService) {
        this.roomTypeService = roomTypeService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(RoomTypeEntity::getId);
        entityOrderFields.add(RoomTypeEntity::getBedTypeName);
        entityOrderFields.add(RoomTypeEntity::getBedPrice);
        entityOrderFields.add(RoomTypeEntity::getRoomPrice);
        entityOrderFields.add(RoomTypeEntity::getEarnestMoney);
        entityOrderFields.add(RoomTypeEntity::getDescribes);
        entityOrderFields.add(RoomTypeEntity::getGmtCreate);
    }

    /**
     * 获取全部的 <strong>房间类型</strong> 列表
     */
    @PreAuthorize("hasAnyRole('ADMIN', 'ROOM.TYPE') or hasAnyAuthority('room.type.list')")
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, RoomTypeEntity entity) {
        LambdaQueryChainWrapper<RoomTypeEntity> lambdaQuery = buildLambdaQuery(entity);

        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>房间类型</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @PreAuthorize("hasAnyRole('ADMIN', 'ROOM.TYPE') or hasAnyAuthority('room.type.list')")
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, RoomTypeEntity entity) {
        LambdaQueryChainWrapper<RoomTypeEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<RoomTypeEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>房间类型</strong>
     *
     * @param id 主键ID
     */
    @PreAuthorize("hasAnyRole('ADMIN', 'ROOM.TYPE') or hasAnyAuthority('room.type.info')")
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(roomTypeService.getById(id));
    }

    /**
     * 添加一个 <strong>房间类型</strong>
     *
     * @param entity 修改后的信息
     */
    @PreAuthorize("hasAnyRole('ADMIN', 'ROOM.TYPE') or hasAnyAuthority('room.type.add')")
    @PostMapping
    public Object add(@RequestBody RoomTypeEntity entity) {
        roomTypeService.saveBedType(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>房间类型</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PreAuthorize("hasAnyRole('ADMIN', 'ROOM.TYPE') or hasAnyAuthority('room.type.update')")
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody RoomTypeEntity entity) {
        entity.setId(id);
        roomTypeService.updateBedType(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>房间类型</strong>
     *
     * @param id 主键ID
     */
    @PreAuthorize("hasAnyRole('ADMIN', 'ROOM.TYPE') or hasAnyAuthority('room.type.delete')")
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        roomTypeService.deleteBedType(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>房间类型</strong>
     *
     * @param ids 主键ID列表
     */
    @PreAuthorize("hasAnyRole('ADMIN', 'ROOM.TYPE') or hasAnyAuthority('room.type.delete')")
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        roomTypeService.deleteBedType(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<RoomTypeEntity> buildLambdaQuery(RoomTypeEntity entity) {
        LambdaQueryChainWrapper<RoomTypeEntity> lambdaQuery = roomTypeService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), RoomTypeEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getBedTypeName()), RoomTypeEntity::getBedTypeName, entity.getBedTypeName());
        lambdaQuery.like(entity.getBedPrice() != null, RoomTypeEntity::getBedPrice, entity.getBedPrice());
        lambdaQuery.like(entity.getRoomPrice() != null, RoomTypeEntity::getRoomPrice, entity.getRoomPrice());
        lambdaQuery.like(entity.getEarnestMoney() != null, RoomTypeEntity::getEarnestMoney, entity.getEarnestMoney());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDescribes()), RoomTypeEntity::getDescribes, entity.getDescribes());
        lambdaQuery.like(entity.getGmtCreate() != null, RoomTypeEntity::getGmtCreate, entity.getGmtCreate());
        return lambdaQuery;
    }

}
